# ~~~
# ####################################################################
# Description:  MachinekitHALUNMANAGED-HALComponent.cmake.in
#
#               This file, 'MachinekitHALUNMANAGED-HALComponent.cmake.in',
#               implements build system
#               rules for Machinekit-HAL project
#
# Copyright (C) 2021    Jakub Fišer  <jakub DOT fiser AT eryaf DOT com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#
# #####################################################################
# ~~~

# Set the variable prefix of this file to 'MHURC_'

set(MHURC_targets_paths
    "[
    {
        \"target\":\"@MACHINEKIT_HAL_NAMESPACE@::runtime_config\",
        \"file\":\"${CMAKE_CURRENT_LIST_DIR}/MachinekitHALRuntimeConfigTarget.cmake\"
    },
    {
        \"target\":\"@MACHINEKIT_HAL_NAMESPACE@::mkini\",
        \"file\":\"${CMAKE_CURRENT_LIST_DIR}/MachinekitHALMKIniTarget.cmake\"
    },
    {
        \"target\":\"@MACHINEKIT_HAL_NAMESPACE@::hal_api\",
        \"file\":\"${CMAKE_CURRENT_LIST_DIR}/MachinekitHALHALAPITarget.cmake\"
    },
    {
        \"target\":\"@MACHINEKIT_HAL_NAMESPACE@::runtime_api\",
        \"file\":\"${CMAKE_CURRENT_LIST_DIR}/MachinekitHALRuntimeAPITarget.cmake\"
    },
    {
        \"target\":\"@MACHINEKIT_HAL_NAMESPACE@::runtime_memory_api\",
        \"file\":\"${CMAKE_CURRENT_LIST_DIR}/MachinekitHALRuntimeMemoryAPITarget.cmake\"
    },
    {
        \"target\":\"@MACHINEKIT_HAL_NAMESPACE@::rtapi_compat\",
        \"file\":\"${CMAKE_CURRENT_LIST_DIR}/MachinekitHALRuntimeCompatTarget.cmake\"
    },
    {
        \"target\":\"@MACHINEKIT_HAL_NAMESPACE@::unmanaged_runtime\",
        \"file\":\"${CMAKE_CURRENT_LIST_DIR}/MachinekitHALUnmanagedRuntimeTarget.cmake\"
    },
]")

if(@SYS_IO_AVAILABLE@)
  set(MHURC_components_paths
      "[
    {
        \"component\":\"Unmanaged-Runtime-PCI\",
        \"file\":\"${CMAKE_CURRENT_LIST_DIR}/MachinekitHALUnmanaged-Runtime-PCIComponent.cmake\"
    },
]")
endif()

string(JSON MHURC_targets_paths_length LENGTH "${MHURC_targets_paths}")
math(EXPR MHURC_targets_paths_length "${MHURC_targets_paths_length} - 1"
     OUTPUT_FORMAT DECIMAL)

if(@SYS_IO_AVAILABLE@)
  string(JSON MHURC_components_paths_length LENGTH "${MHURC_components_paths}")
  math(EXPR MHURC_components_paths_length "${MHURC_components_paths_length} - 1"
       OUTPUT_FORMAT DECIMAL)

  foreach(_index RANGE 0 ${MHURC_components_paths_length} 1)
    string(JSON MHURC_path GET "${MHURC_components_paths}" ${_index} "file")
    if(NOT EXISTS "${MHURC_path}")
      set(${CMAKE_FIND_PACKAGE_NAME}_FOUND False)
      set(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE
          "File \"${MHURC_path}\" is missing!")
      return()
    endif()
    include("${MHURC_path}")
  endforeach()
endif()

foreach(_index RANGE 0 ${MHURC_targets_paths_length} 1)
  string(JSON MHURC_name GET "${MHURC_targets_paths}" ${_index} "target")
  string(JSON MHURC_path GET "${MHURC_targets_paths}" ${_index} "file")
  if(NOT EXISTS "${MHURC_path}")
    set(${CMAKE_FIND_PACKAGE_NAME}_FOUND False)
    set(${CMAKE_FIND_PACKAGE_NAME}_NOT_FOUND_MESSAGE
        "File \"${MHURC_path}\" is missing!")
    return()
  endif()
  if(NOT TARGET "${MHURC_name}")
    include("${MHURC_path}")
  endif()
endforeach()
